import BaseUtil from '@/BaseUtil';
import BuffUtil from '@/BuffUtil';
import MathUtil from '@/MathUtil';
import RandomUtil from '@/RandomUtil';
import SelectUtil from '@/SelectUtil';
import BuffCommon from 'code/Core/Common/BuffCommon';
import MathCommon from 'code/Core/Common/MathCommon';
import MovementCommon from 'code/Core/Common/MovementCommon';
import SLKCommon from 'code/Core/Common/SLKCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 杀戮盛宴
 **/
export default class DZ_KillingSpree extends AbilityBase {
    static instance: DZ_KillingSpree;
    buffType: AppBuffType = {
        id: 'buff_DZ_KillingSpree',
        art: this.art,
        name: this.abilityName,
        ubertip: '正在快速对周围的敌人发动多次攻击,期间免疫所有伤害。',
        dur: 4,
        attribute: {
            全伤害减免: 9,
        },
        targetAttach: 'hand',
        targetArt: 'Abilities\\Weapons\\PhoenixMissile\\Phoenix_Missile_mini.mdl',
    };
    constructor(id: string = 'Asly') {
        super(id);
        DZ_KillingSpree.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        BuffCommon.addBuff({ target: source, caster: source, buffTypeId: this.buffType.id, ablityId: this.id });
        BuffCommon.addBuff({ target: source, caster: source, buffTypeId: StunSystemIntance.unEffectBuffId, ablityId: this.id });
        let lv = this.getAbilityLevel(source);
        if (IsUnitType(source, UNIT_TYPE_MELEE_ATTACKER)) {
            // 近战处理
            this.meleeProcess(source);
            let count = MathCommon.getAbilityEffectCount(4 + 2 * lv, source, this.id);
            let t = 4 / count;
            t = Math.max(0.15, t);
            BaseUtil.onTimer(t, (c) => {
                return c < count && this.meleeProcess(source);
            });
        } else {
            tarX = GetUnitX(source);
            tarY = GetUnitY(source);
            // 远程处理
            this.rangedProcess(source, tarX, tarY);
            let count = MathCommon.getAbilityEffectCount(2 + lv, source, this.id);
            let t = 4 / count;
            BaseUtil.onTimer(t, (c) => {
                return c < count && this.rangedProcess(source, tarX, tarY);
            });
        }
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    rangedProcess(source: unit, ox: number, oy: number) {
        let tars = SelectUtil.getEnemyUnitsInRange(source, 450, ox, oy);
        if (tars.length == 0) {
            return false;
        }
        let { x, y } = MathUtil.polarProjection(ox, oy, 550, RandomUtil.randomAngle());
        SetUnitX(source, x);
        SetUnitY(source, y);
        let angle = MathUtil.angleBetweenCoords(x, y, ox, oy);
        EXSetUnitFacing(source, angle);
        DestroyEffect(AddSpecialEffectTarget('Abilities\\Spells\\NightElf\\Blink\\BlinkCaster.mdl', source, 'origin'));
        SetUnitAnimation(source, 'Attack');
        for (let e of tars) {
            MovementCommon.TargetMissile({
                missile: SLKCommon.getUnitMissileart(source),
                caster: source,
                speed: 900,
                radian: SLKCommon.getUnitMissilearc(source),
                target: e,
                onReach: (p) => {
                    DamageSystemInstance.applyAttackDamage(e, p.caster, UnitCommon.取攻击(p.caster) + 20 * GetUnitAbilityLevel(p.caster, this.id), this.id);
                    DestroyEffect(AddSpecialEffectTarget('405.mdx', e, 'origin'));
                },
            });
        }
        return true;
    }
    meleeProcess(source: unit) {
        let tar = SelectUtil.getRandomEnemyInRange(source, 450);
        if (!tar) {
            return false;
        }
        let { x, y } = MathUtil.polarProjection(GetUnitX(tar), GetUnitY(tar), 200, RandomUtil.randomAngle());
        let angle = MathUtil.angleBetweenCoords(GetUnitX(source), GetUnitY(source), x, y);
        EXSetUnitFacing(source, angle);
        MovementCommon.hitFly({
            curX: x,
            curY: y,
            target: source,
            caster: source,
            distance: 400,
            time: 0.2,
            maxHeight: 0,
            angle,
        });
        SetUnitAnimation(source, 'Slam');
        DamageSystemInstance.applyAttackDamage(tar, source, UnitCommon.取攻击(source) + 40 * GetUnitAbilityLevel(source, this.id), this.id);
        DestroyEffect(AddSpecialEffectTarget('405.mdx', tar, 'origin'));
        DestroyEffect(AddSpecialEffectTarget('Abilities\\Spells\\NightElf\\Blink\\BlinkCaster.mdl', source, 'origin'));
        PlaySound('Sound\\Units\\Combat\\MetalHeavySliceStone2.wav');
        return true;
    }
}
